package com.sss.myspringsecurity.util;

import io.jsonwebtoken.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author sss
 * @date 2020-02-09 21:09
 **/
public class JwtUtil {

    private  static String secret="asdasdasdrewtgb782635";

    private static int expireTime=30 * 60 * 1000;

    public static String createToken(String name){
        HashMap<String, Object> claims = new HashMap<>();
        claims.put("username",name);
      return Jwts.builder().setClaims(claims).signWith(SignatureAlgorithm.HS256,secret)
                .setExpiration(new Date(System.currentTimeMillis()+expireTime)).compact();
    }

    public static Map<String, Object> paresToken(String token){
        HashMap<String, Object> map = new HashMap<>();
        Claims claimsJws = Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
        long time = claimsJws.getExpiration().getTime();
        map.put("expireTime",time);
        for (Map.Entry<String, Object> entry : claimsJws.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            map.put(key,value);
        }
        return map;
    }
}
